﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Tamir.SharpSsh;
using Librerias;
using System.Data.SqlClient;
using System.IO;

namespace Aplicacion.admin.procesos
{
    public partial class backupDB : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Directory.Exists(@"c:\SQLBackup"))
            {
                if (File.Exists(@"c:\SQLBackup\backupAppDb.bak"))
                {
                    File.Delete(@"c:\SQLBackup\backupAppDb.bak");
                }
            } else
                Directory.CreateDirectory(@"c:\SQLBackup");

            try
            {
                //Connect to DB
                SqlConnection connect;
                string con = @"Server=.\SQLEXPRESS;Initial Catalog=Aplicacion.Modelos.AppDb;Integrated Security=True;";
                connect = new SqlConnection(con);
                connect.Open();
                //----------------------------------------------------------------------------------------------------

                //Execute SQL---------------
                SqlCommand command;
                command = new SqlCommand(@"BACKUP DATABASE [Aplicacion.Modelos.AppDb] TO DISK = 'c:\SQLBackup\backupAppDb.bak' WITH INIT,STATS=10", connect);
                command.ExecuteNonQuery();
                //-------------------------------------------------------------------------------------------------------------------------------

                connect.Close();
                Notificaciones.Correcto(@"Backup realizado correctamente en c:\SQLBackup\backupAppDb.bak.");
                Response.Redirect("~/admin");
            }
            catch (SqlException ex)
            {
                Notificaciones.Error(ex.Message);
                Response.Redirect("~/admin");
            }
        }
    }
}